package com.yunqiao.service.api.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunqiao.common.consts.AppConsts;
import com.yunqiao.common.utils.PageUtils;
import com.yunqiao.common.utils.Query;
import com.yunqiao.service.api.FundDetailService;
import com.yunqiao.service.api.entity.ApplyPartnerEntity;
import com.yunqiao.service.api.entity.FundDetailEntity;
import com.yunqiao.service.api.mapper.ApplyPartnerMapper;
import com.yunqiao.service.api.mapper.FundDetailMapper;
import com.yunqiao.service.sys.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("fundDetailService")
public class FundDetailServiceImpl extends ServiceImpl<FundDetailMapper, FundDetailEntity> implements FundDetailService {

    @Autowired
    private ApplyPartnerMapper applyPartnerMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //各种查询条件的过滤,在此进行
        QueryWrapper<FundDetailEntity> queryWrapper = new QueryWrapper<>();
        if (params.get("userId") != null) {
            queryWrapper.eq("user_id", params.get("userId"));
        }
        //代理商
        if (params.get("sysType") != null && MapUtil.getBool(params, "sysType")) {
            SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();

            if (user.getType().equals(AppConsts.SYS_USER_TYPE_1)) {
                ApplyPartnerEntity applyPartnerEntity = applyPartnerMapper.selectOne(new QueryWrapper<ApplyPartnerEntity>().eq("state", AppConsts.USER_APPLY_PARTNER_STATE_3)
                    .eq("sys_user_id", user.getUserId()));
                //代理商
                queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("user_id", user.getUserId());
                queryWrapper.eq("type_from", AppConsts.TYPE_FORM_1);
            }
        }



        IPage<FundDetailEntity> page = this.page(new Query<FundDetailEntity>(params).getPage(), queryWrapper);
        return new PageUtils(page);
    }

}
